package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.IView.InRegionListView;
import com.cloudsigns.factorymanager2.engineering.adapter.RegionListAdapter;

import com.cloudsigns.factorymanager2.engineering.bean.RegionBean;
import com.cloudsigns.factorymanager2.engineering.presenter.RegionListPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/5.
 */

public class RegionListActivity extends Activity implements View.OnClickListener, InRegionListView {
    private RelativeLayout rl_right;
    private ImageView img_right;
    private TextView tv_title;
    private ListView lv;
    private ViewGroup rl_back;
    private RegionListAdapter adapter;
    private List<RegionBean> regionBeanList = new ArrayList<>();
    private RegionListPresenter mediaRegionListPresenter;
    private final String TAG = RegionListActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_create_engineering_order_list);

        mediaRegionListPresenter = new RegionListPresenter(this);
        init();

        getMediaRegionList();

    }

    private void init() {

        rl_right = findViewById(R.id.rl_right);
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        img_right = findViewById(R.id.img_right);
        lv = findViewById(R.id.lv);

        tv_title.setText(getResources().getText(R.string.media));
        img_right.setBackgroundResource(R.mipmap.title_icon_menu);
        rl_right.setVisibility(View.VISIBLE);
        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);

        adapter = new RegionListAdapter(this, TAG);
        adapter.setData(regionBeanList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent mIntent;
                RegionBean order = regionBeanList.get(position);

                mIntent = new Intent(RegionListActivity.this, EditMediaActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, order);
                startActivity(mIntent);

            }
        });
    }

    private void getMediaRegionList() {
        mediaRegionListPresenter.getMediaRegionList();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;

        switch (view.getId()) {
            case R.id.rl_right:
                mIntent = new Intent(RegionListActivity.this, AddMediaActivity.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);

                break;
            case R.id.rl_back:
                finish();
                break;


        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.CREATED_ORDER: //创建媒体
                if (data != null) {
                    Boolean update = (Boolean) data.getBooleanExtra(ConstantUtil.KEY_1, false);
                    if (update) {
                        getMediaRegionList();
                    }


                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void getMediaRegionListSuccessed(List<RegionBean> regionList) {
        if (null != regionList) {
            regionBeanList.clear();
            regionBeanList.addAll(regionList);
            adapter.setData(regionBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
